package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.aspectj.lang.enums.OperatorType;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/mobileUser/")
@Api(tags = "城市定位和根据城市id获取城市分类", description = "城市定位和根据城市id获取城市分类")
public class CityController extends BaseController {

    @Autowired
    private ISysCityService sysCityService;

    @Autowired
    private ISysTypeService sysTypeService;

    @Autowired
    private ISysTypeDetailService sysTypeDetailService;

    @Autowired
    private ISysSkuService sysSkuService;


    @ApiOperation("根据城市名称获取城市id")
    @PostMapping("/getByCityName")
    @Log(title = "根据城市名称获取城市id", operatorType = OperatorType.MOBILE)
    @RepeatSubmit
    @SignatureValidation
    public AjaxResult getByCityName(@RequestBody SysCity sysCity) {
        if (null == sysCity) {
            return AjaxResult.error("参数错误");
        }
        if (sysCity.getCityName().isEmpty()) {
            return AjaxResult.error("城市名称错误");
        }
        List<SysCity> list = sysCityService.getByCityName(sysCity);
        if (list.isEmpty()) {
            return AjaxResult.error("暂无此城市");
        } else {
            return AjaxResult.success(list.get(0));
        }
    }

    @ApiOperation("根据城市id获取已关联的分类")
    @PostMapping("/getTypeByCityId")
    @Log(title = "根据城市id获取已关联的分类", operatorType = OperatorType.MOBILE)
    @SignatureValidation
    public AjaxResult getTypeByCityId(@RequestBody SysType sysType) {
        if (null == sysType) {
            return AjaxResult.error("参数错误");
        }
        if (null == sysType.getCityId()) {
            return AjaxResult.error("城市ID未传");
        }
        List<SysType> list = sysTypeService.getTypeByCityId(sysType);
        return AjaxResult.success(sysTypeService.buildTypeTree(list));
    }

    @ApiOperation("通过分类id获取分类详情")
    @PostMapping("/getDetailByTypeId")
    @Log(title = "通过分类id获取分类详情", operatorType = OperatorType.MOBILE)
    @SignatureValidation
    public AjaxResult getDetailByTypeId(@RequestBody SysTypeDetail sysTypeDetail) {
        if (null == sysTypeDetail) {
            return AjaxResult.error("参数错误");
        }
        if (null == sysTypeDetail.getTypeDetailId()) {
            return AjaxResult.error("分类详情id未传");
        }
        SysTypeDetail result = sysTypeDetailService.selectSysTypeDetailById(sysTypeDetail.getTypeDetailId());
        if (null != result) {
            SysSku sysSku = new SysSku();
            sysSku.setTypeDetailId(result.getTypeDetailId());
            result.setSku(sysSkuService.selectSysSkuList(sysSku));
        }

        return AjaxResult.success(result);
    }

    @GetMapping("/getList")
    @ApiOperation("按字母排序")
    public AjaxResult getList() {
        List<SysCity> sysCities = sysCityService.selectGetList();
        Map<CharSequence, List<SysCity>> collect = sysCities.stream().collect(Collectors.groupingBy(sysCity -> sysCity.getPinyin().substring(0, 1)));
        return AjaxResult.success(collect);
    }
}
